package com.erp.auto.controller;

import com.erp.auto.domain.ErpAutoUnshelfProductConfig;
import com.erp.auto.service.ErpAutoUnshelfProductConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 下架商品配置表
 *
 * @author WXX
 * @date 2024-01-16 10:04:56
 */
@RestController
@AllArgsConstructor
@RequestMapping("/auto/unshelf")
public class ErpAutoUnshelfProductConfigController {

    private final ErpAutoUnshelfProductConfigService erpAutoUnshelfProductConfigService;

    /**
     * 保存
     */
    @PostMapping("/saveUnShelfConfig")
    public R<Object> saveUnShelfConfig(@RequestBody ErpAutoUnshelfProductConfig config) {
        Boolean flag = erpAutoUnshelfProductConfigService.saveUnShelfConfig(config);
        return flag ? R.ok() : R.fail();
    }

    @GetMapping("/setStatus")
    public R<Object> setStatus(@Param("id") Long id) {
        ErpAutoUnshelfProductConfig config = erpAutoUnshelfProductConfigService.selectById(id);
        if (config.getStatus() == 0) {
            config.setStatus(1);
        }else {
            config.setStatus(0);
        }
        Boolean flag = erpAutoUnshelfProductConfigService.saveUnShelfConfig(config);
        return flag ? R.ok() : R.fail();
    }

    /**
     * 获取列表
     */
    @GetMapping("/getList")
    public R<Object> getList( @RequestParam(defaultValue = "1") Integer pageNum,
    @RequestParam(defaultValue = "10") Integer pageSize,
    ErpAutoUnshelfProductConfig config) {

        PageHelper.startPage(pageNum, pageSize);
        List<ErpAutoUnshelfProductConfig> list = erpAutoUnshelfProductConfigService.getList(config);
        PageInfo<ErpAutoUnshelfProductConfig> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    @GetMapping("/getImplementList")
    R<Object> getImplementList(@RequestParam(value = "storeId", required = false) Long storeId) {
        ErpAutoUnshelfProductConfig config = new ErpAutoUnshelfProductConfig();
        config.setStatus(1);
        if (storeId != null) 
            config.setStoreId(storeId);
        List<ErpAutoUnshelfProductConfig> list = erpAutoUnshelfProductConfigService.getList(config);
        return list.size() > 0? R.ok(list): R.fail();
    }

    @GetMapping("/selectById")
    public R<Object> selectById(@Param("id") Long id) {
        ErpAutoUnshelfProductConfig config = erpAutoUnshelfProductConfigService.selectById(id);
        return R.ok(config);
    }

    /**
     * 执行自动下架
     * @return
     */
    @PostMapping("/unShelfProduct")
    public R<Object> unShelfProduct(@RequestBody ErpAutoUnshelfProductConfig config) {
        erpAutoUnshelfProductConfigService.autoUnShelfProductService(config);
        return R.ok();
    }
}
